<template>
  <header class="custom-header">
    <div class="header-content">
      <div class="logo">
        <h1>WChat</h1>
      </div>
      <div class="nav-menu">
        <div class="nav-links">
          <button
            class="nav-link"
            :class="{ active: currentRoute === 'home' }"
            @click="navigateTo('home')"
          >
            <HomeOutlined /> 首页
          </button>
          <div class="contact-container">
            <button
              class="nav-link"
              :class="{ active: currentRoute === 'contact' }"
              @click="handleContactClick"
            >
              <UserOutlined /> 联系人
            </button>
            <span v-if="messageCount > 0" class="message-badge">
              {{ messageCount }}
            </span>
          </div>
        </div>
      </div>
      <div class="user-info">
        <a-dropdown>
          <a-button type="text" class="user-button">
            <UserOutlined />
            <span>{{ userInfo.nickName || userInfo.email }}</span>
            <DownOutlined />
          </a-button>
          <template #overlay>
            <a-menu>
              <a-menu-item @click="handleLogout">
                <LogoutOutlined />
                退出登录
              </a-menu-item>
            </a-menu>
          </template>
        </a-dropdown>
      </div>
    </div>
  </header>
</template>

<script setup lang="ts">
import { defineProps, defineEmits, ref, onMounted, onUnmounted } from 'vue'
import { UserOutlined, DownOutlined, LogoutOutlined, HomeOutlined } from '@ant-design/icons-vue'
import { useRouter, useRoute } from 'vue-router'
import { Menu, message } from 'ant-design-vue'

// 定义props
interface UserInfo {
  nickName?: string
  email?: string
}

defineProps<{
  userInfo: UserInfo
}>()

// 定义emits
const emit = defineEmits<{
  logout: []
}>()

const router = useRouter()
const route = useRoute()
const currentRoute = ref('home')
const messageCount = ref(0)

// 监听路由变化
onMounted(() => {
  listening()
  currentRoute.value = route.path === '/contact' ? 'contact' : 'home'

  const unsubscribe = router.afterEach((to) => {
    currentRoute.value = to.path === '/contact' ? 'contact' : 'home'
  })

  return () => unsubscribe()
})

// 处理导航选择
const handleNavSelect = (key: string) => {
  if (key === 'home') {
    router.push('/home')
  } else if (key === 'contact') {
    router.push('/contact')
  }
}

// 处理联系人点击，清零消息数
const handleContactClick = () => {
  navigateTo('contact')
  messageCount.value = 0
}

// 简化的导航方法
const navigateTo = (route: string) => {
  console.log('导航到:', route)
  if (route === 'home') {
    router.push('/home')
  } else if (route === 'contact') {
    router.push('/contact')
  }
}
const listening = () => {
  window.electron.ipcRenderer.on('friendRequest', (event, request) => {
    console.log('收到好友申请:', request.messageContent)
    console.log('好友申请详情:', JSON.stringify(request))

    // 增加消息计数
    messageCount.value++
  })
}
// 组件卸载时移除监听
onUnmounted(() => {
  window.electron.ipcRenderer.removeAllListeners('friendRequest')
})
const handleLogout = (): void => {
  emit('logout')
}
</script>

<style scoped>
.custom-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid rgba(226, 232, 240, 0.8);
  padding: 0 32px;
  height: 72px;
  line-height: 72px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  max-width: 1600px;
  margin: 0 auto;
}

.nav-menu {
  flex: 1;
  display: flex;
  justify-content: center;
  margin: 0 32px;
}

.nav-links {
  display: flex;
  gap: 16px;
}

.contact-container {
  position: relative;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 20px;
  border: none;
  background: none;
  font-size: 16px;
  font-weight: 500;
  color: #666;
  cursor: pointer;
  border-radius: 20px;
  transition: all 0.2s ease;
}

.nav-link:hover {
  background-color: rgba(102, 126, 234, 0.05);
  color: #667eea;
}

.nav-link.active {
  color: #667eea;
  background-color: rgba(102, 126, 234, 0.05);
}

.message-badge {
  position: absolute;
  top: 16px;
  left: 90px;
  background-color: #ff4d4f;
  color: white;
  font-size: 12px;
  font-weight: bold;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.logo h1 {
  margin: 0;
  color: #667eea;
  font-size: 28px;
  font-weight: 700;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.user-button {
  display: flex;
  align-items: center;
  gap: 12px;
  height: 48px;
  padding: 0 20px;
  border-radius: 24px;
  border: 1px solid #e2e8f0;
  background: white;
  transition: all 0.2s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.user-button:hover {
  border-color: #667eea;
  box-shadow: 0 4px 16px rgba(102, 126, 234, 0.2);
  transform: translateY(-1px);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .custom-header {
    padding: 0 20px;
    height: 64px;
    line-height: 64px;
  }

  .message-badge {
    left: 80px;
    top: 12px;
  }
}
</style>
